package com.fivestar.pmanager.core.util;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;

/**
 * 
 * 数字处理工具类
 * 
 */
public final class NumberUtil {

    /**
     * 对double数据进行取精度.
     * <p/>
     * For example: <br/>
     * double value = 100.345678; <br/>
     * double ret = round(value,4,BigDecimal.ROUND_HALF_UP); <br/>
     * ret为100.3457 <br/>
     *
     * @param value        double数据.
     * @param scale        精度位数(保留的小数位数).
     * @param roundingMode 精度取值方式.
     *                     java.math.BigDecimal.ROUND_UP <br/>
     *                     java.math.BigDecimal.ROUND_DOWN <br/>
     *                     java.math.BigDecimal.ROUND_CEILING <br/>
     *                     java.math.BigDecimal.ROUND_FLOOR <br/>
     *                     java.math.BigDecimal.ROUND_HALF_UP<br/>
     *                     java.math.BigDecimal.ROUND_HALF_DOWN <br/>
     *                     java.math.BigDecimal.ROUND_HALF_EVEN <br/>
     * @return 精度计算后的数据.
     */
    public static double round(double value, int scale, int roundingMode) {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(scale, roundingMode);
        double d = bd.doubleValue();
        bd = null;
        return d;
    }

    public static double round(double value, int scale) {
        return round(value, scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 向上取整除法
     * <p/>
     * For example: <br/>
     * ceilDevide(10, 2) == 5; <br/>
     * ceilDevide(10, 3) == 4; <br/>
     *
     * @param dividend 被除数
     * @param divisor  除数
     * @return 向上取整的商.
     */
    public static int ceilDevide(int dividend, int divisor) {
        int quotient = dividend / divisor;
        return (dividend % divisor != 0) ? quotient + 1 : quotient;
    }

    /**
     * 向上取整除法
     * <p/>
     * For example: <br/>
     * ceilDevide(10, 2) == 5; <br/>
     * ceilDevide(10, 3) == 4; <br/>
     *
     * @param dividend 被除数
     * @param divisor  除数
     * @return 向上取整的商.
     */
    public static int min(int dividend, int divisor) {
        int quotient = dividend / divisor;
        return (dividend % divisor != 0) ? quotient + 1 : quotient;
    }


    /**
     * 生成日期加随机数的序列号
     *
     * @return
     * @throws
     */
    public static String generateSerialNumber()
    {
        String date = DateHelper.formatDate(new Date(), "yyyyMMddHHmmss");
        String random = String.valueOf(Math.random());
        random = random.substring(2, 9);
        return date + random;
    }

    /**
     * 生成前缀加日期加随机数的序列号
     *
     * @param prefix
     * @return
     */
    public static String generateSerialNumber(String prefix)
    {
        String date = DateHelper.formatDate(new Date(), "yyMMdd");
        String random = String.valueOf(Math.random());
        if(null == prefix)
            return date+random.substring(2, 12);
        return prefix + date+ random.substring(2, 7);
    }


    /**
     * 生产指定长度数字随机数
     *
     * @param length
     * @return
     */
    public static String generateLengthNumber(int length)
    {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i< length; i++)
        {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    /**
     * 生成前缀+随机数+时间戳的字符串
     *
     * @param prefix
     * @return
     */
    public static String generateVoucherCode(String prefix)
    {
        String date = DateHelper.formatDate(new Date(), "yyMMddHHmmss");
        String random = String.valueOf(Math.random());
        if(null == prefix)
            return random.substring(2, 4) +date;
        return prefix + random.substring(2, 4) +date;
    }

}
